For this MLRS 101, we are going to look R-studio for the purpose when RM asked us like “Any updates?” then we can proudly show some plots or data (like Rachel Green). ;)
Goal of today’s MLRS 101 is learn how to process data, obtain statistical results and produce plots with R-studio.
There are a few chapters,
Before you write any code, please download and install R and R-studio as instructed here.
This is a website that includes more detailed instruction on how to install R-studio, and also has some good “hands-on” tutorials.
If you are confident that you already know the answer these questions you can skip reading the chapter. Don’t obsess with understanding everything now, but use the chapter to familiarize yourself with new vocabulary terms and the interface of your R-studio playground.
For example “The R User Interface” will tell you VERY VERY basic of R-studio; basic interface and where to type your code. If you are able to see R-studio screen similar to the following image & you know where to type your code then you are ready to go!
Q: What would do you if do you want to play Super Mario with a Nintendo Switch?
A: You need to buy a Nintendo Switch and buy a Super Mario game pack. Or find one of each that fell off the back of a truck.
Q: Do we need to buy anything to use R?
A: We will not need to pay to work in R. We only need to download R & R-studio and packages like dplyr, ggplot2, and pdacR. Sometimes you can even find an existing supported “library” on Cran-R or an open-source project on GitHub that will do almost exactly what you need it to do. And you didn’t even have to wait for anything to fall off a truck!
To enumerate the Super Mario metaphor…
Nintendo Switch == R-studio on your computer
Super Mario game == R packages
Mario jump == ggplot() function
Before we write some commands, we have to create a location where we will put our code. Open R-Studio, please open a new R-script with ctrl (command) + n like the following screenshot and also press ctrl (command) + s to create the file on your disk.
Write the following code to your script and check the “Console” like the following screenshot.
We need this package (game) to manipulate our data as we wished.
install.packages("tidyverse")
This is our favorite visualization package (game)!
install.packages("ggplot2")
The rest of these packages (games) are for fancy visualization :)
install.packages("ggpmisc")
install.packages("gganimate")
install.packages("animation")
install.packages("kableExtra")
Once you’ve run all the install.packages() codes, you should see “tidyverse”, “ggplot2”, “ggpmisc”, “gganimate”, and “animation” listed in the “Packages” tab at the bottom-right of your RStudio environment. If these packages aren’t visible there, try running the commands in the console.
To run code, simply click the “Run” button at the top of your R script.
Like Super Mario game, once you install (buy) you do not need
to re-install to your machine again! [edit: struck because you
may have to update your version of R (and R-Studio) from time to time to
be compatible with the latest and greatest packages or to have new
features available to you]
We’ve now installed packages (Super Mario packs) to our R-studio (Nintendo switch), but if we want to jump and get mushrooms with Mario we need to start playing a game!
Now if you want to play RM’s game, you should initiate RM’s game which includes multiple packages. Run the following codes (library(…)) at the Console tab at the bottom to start this game.
# Load the tidyverse package
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Load the ggplot2 package
library(ggplot2)
# Load the ggpmisc package
library(ggpmisc)
## Warning: package 'ggpmisc' was built under R version 4.3.3
## Loading required package: ggpp
## Registered S3 methods overwritten by 'ggpp':
## method from
## heightDetails.titleGrob ggplot2
## widthDetails.titleGrob ggplot2
##
## Attaching package: 'ggpp'
##
## The following object is masked from 'package:ggplot2':
##
## annotate
##
## Registered S3 method overwritten by 'ggpmisc':
## method from
## as.character.polynomial polynom
# Load the gganimate package
library(gganimate)
# Load the animation package
library(animation)
# Load another animation package
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
install the packages again, but just use
the library() commands to start the game!I am very excited that you read and ran the R codes with us today. But one of the most important things is to learn how to turn off your R-studio (Nintendo switch). If on a Switch you already defeated 10 levels and quit for the day, it’d be soooo annoying to have to start back on the first level the next day. This is why we are using a Switch and not an old school NES. So let’s save the stage of our game (code) as of “R-script” file.
Like the following screenshot, if you haven’t saved your R code
script since your last modification (or ever!) the title of the window
will be suffixed with an asterisk. So, if you have a window labelled
“Untitled1*” the code will need to be saved to disk in order for you to
return to it.
Press ctrl (command) + s to save the file. If you haven’t previously saved the file, you will be presented with the Save As dialogue. Select a folder on your computer where you want to save your code. Then type a file name. This name can be just about anything. For this example we will name it my_first_R_script.
Thank you for following along with our lesson!
– We successfully installed R-studio and familiarized ourselves with its interface.
Starting in Part 2. we are going explore using R-studio with actual data.
Here are some key points for you to consider - these will be great review questions:
++ ends with how to turn off the R-studio and give them a homework to review the structure and interface of R-studio